package com.toijng.weixintracker.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;

/**
 * Created by lizhuo on 16/7/29.
 */
public class LoggerInterceptor implements Interceptor {
    public static final String TAG = "OkHttpUtils";
    private String tag;
    private boolean showResponse;
    private boolean showRequest = true;

    public static final int NONE = -1;
    public static final int BODY = 0;
    public static final int HEAD = 2;
    public static final int BOTH = 3;

    private int level = BODY;

    public LoggerInterceptor(String tag, boolean showResponse) {
        if (TextUtils.isEmpty(tag)) {
            tag = TAG;
        }
        this.showResponse = showResponse;
        this.tag = tag;
    }

    public void setShowRequest(boolean showRequest) {
        this.showRequest = showRequest;
    }

    public void setLevel(int level) {
        if (level >= -1 && level <= 3) {
            this.level = level;
        }
    }

    public LoggerInterceptor(String tag) {
        this(tag, false);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        logForRequest(request);
        Response response = chain.proceed(request);


        return logForResponse(response);
    }

    private Response logForResponse(Response response) {
        try {
            if (level == NONE) {
                return response;
            }
            //===>response log
            Log.e(tag, "========response'log=======");
            Response.Builder builder = response.newBuilder();
            Response clone = builder.build();
            Log.e(tag, "url : " + clone.request().url());
            Log.e(tag, "code : " + clone.code());
            Log.e(tag, "protocol : " + clone.protocol());
            if (!TextUtils.isEmpty(clone.message()))
                Log.e(tag, "message : " + clone.message());

            if (showResponse) {
                if (level == HEAD || level == BOTH) {
                    Headers headers = response.headers();
                    if (headers != null && headers.size() > 0) {
                        Log.e(tag, "headers : " + headers.toString());
                    }
                }


                if (level == BODY || level == BOTH) {
                    ResponseBody body = clone.body();
                    if (body != null) {
                        MediaType mediaType = body.contentType();
                        if (mediaType != null) {
                            Log.e(tag, "responseBody's contentType : " + mediaType.toString());
                            if (isText(mediaType)) {
                                String resp = body.string();
                                Log.e(tag, "responseBody's content : " + resp);

                                body = ResponseBody.create(mediaType, resp);
                                return response.newBuilder().body(body).build();
                            } else {
                                String resp = body.string();
                                Log.e(tag, "responseBody's content : " + resp);
//                                Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!");
                            }
                        }
                    }
                }


            }


        } catch (Exception e) {
//            e.printStackTrace();
        } finally {
            Log.e(tag, "========response'log=======end");
            Log.e(tag, "**********************************************");
            Log.e(tag, "**********************************************");
        }

        return response;
    }

    private void logForRequest(Request request) {
        try {
            if (level == NONE || !showRequest) {
                return;
            }
            String url = request.url().toString();
            Headers headers = request.headers();


            Log.e(tag, "========request'log=======");
            Log.e(tag, "method : " + request.method());
            Log.e(tag, "url : " + url);
            if (level == HEAD || level == BOTH) {
                if (headers != null && headers.size() > 0) {
                    Log.e(tag, "headers : " + headers.toString());
                }
            }
            if (level == BODY || level == BOTH) {
                RequestBody requestBody = request.body();
                if (requestBody != null) {
                    MediaType mediaType = requestBody.contentType();
                    if (mediaType != null) {
                        Log.e(tag, "requestBody's contentType : " + mediaType.toString());
                        if (isText(mediaType)) {
                            Log.e(tag, "requestBody's content : " + bodyToString(request));
                        } else {
                            Log.e(tag, "requestBody's content : " + " maybe [file part] , too large too print , ignored!");
                        }

                    }

                }
            }

            Log.e(tag, "========request'log=======end");
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    private boolean isText(MediaType mediaType) {
        if (mediaType.type() != null && mediaType.type().equals("text")) {
            return true;
        }
        if (mediaType.subtype() != null) {
            if (mediaType.subtype().equals("json") ||
                    mediaType.subtype().equals("xml") ||
                    mediaType.subtype().equals("html") ||
                    mediaType.subtype().equals("webviewhtml")
                    )
                return true;
        }
        return false;
    }

    private String bodyToString(final Request request) {
        try {
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);
            return buffer.readUtf8();
        } catch (final IOException e) {
            return "something error when show requestBody.";
        }
    }


}

